package com.xiaoxi.visitormode.visitor;

import com.xiaoxi.visitormode.composite.Directory;
import com.xiaoxi.visitormode.composite.Entry;
import com.xiaoxi.visitormode.composite.File;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Author xiaoxi
 * @ProjectName design_mode
 * @PackageName com.xiaoxi.visitormode.visitor
 * @ClassName FileFindVisitor
 * @Description TODO
 * @Date 2023/3/16 19:50
 * @Version 1.0
 */
public class FileFindVisitor implements Visitor {

    private String suffix;
    private List<String> list;

    public FileFindVisitor(String suffix) {
        this.suffix = suffix;
        list = new ArrayList<>();
    }

    @Override
    public void visit(File file) {
        if (file.getName().endsWith(suffix)) {
            list.add(file.pwd());
        }
    }

    @Override
    public void visit(Directory directory) {
        Iterator iterator = directory.createIterator();
        while (iterator.hasNext()) {
            Entry entry = (Entry) iterator.next();
            entry.accept(this);
        }
    }

    public Iterator createIterator() {
        return list.listIterator();
    }

}
